package com.xx.doublexchat.common.user.event.listener;

import com.xx.doublexchat.common.chat.service.IPushService;
import com.xx.doublexchat.common.user.dao.UserApplyDao;
import com.xx.doublexchat.common.user.domain.entity.UserApply;
import com.xx.doublexchat.common.user.event.UserApplyEvent;
import com.xx.doublexchat.common.websocket.domain.vo.resp.WSFriendApply;
import com.xx.doublexchat.common.websocket.service.adapter.WebSocketAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;

@Slf4j
@Component
public class UserApplyListener {
    @Autowired
    private UserApplyDao userApplyDao;

    @Autowired
    private IPushService pushService;

    /**
     * 添加好友通知
     *
     * @param event
     */
    @Async
    @TransactionalEventListener(classes = UserApplyEvent.class, fallbackExecution = true)
    public void notifyFriend(UserApplyEvent event) {
        UserApply userApply = event.getUserApply();
        Integer unReadCount = userApplyDao.getUnReadCount(userApply.getTargetId());
        pushService.sendPushMsg(WebSocketAdapter.buildApplySend(new WSFriendApply(userApply.getUid(),
                unReadCount)), userApply.getTargetId());
    }
}
